<template>
	<view>
		<web-view 
			:src="httpUrl+'/ysdAppRender/mobilePageVisit?isMobile=Y&mobileData='+JSON.stringify(sysInfo)"
			update-title="false"
			@message="handleMessage"
			ref="appWebView"
			id="appWebView"
		>
		</web-view>
	</view>
</template>

<script>
import {ApiPackageMixins} from './UniAppApi/ApiPackageMixins.js'
	export default {
		mixins:[ApiPackageMixins],
		data() {
			return {
				// httpUrl:'http://192.168.10.49/YSD',
				// appKey:'55e4ddb1-9c7e-41eb-8ab2-8e5a975ed23f',
				httpUrl: null,
				appKey: null,
				sysInfo:{
					statusBarHeight: 0,
					appKey: null
				}
			}
		},
		onLoad() {
			let that = this;
			uni.getSystemInfo({
				success: function (res) {
					that.sysInfo['statusBarHeight'] = res.statusBarHeight;
				}
			});
			if(!this.httpUrl || !this.appKey){
				const data = uni.getStorageSync('appInfoKey');
				if(data){
					let _data = data;
					if(typeof data === 'string'){
						try{
							_data = JSON.parse(data);
						}catch (e){
							this.handleError()
						}
					}
					this.httpUrl = _data.httpUrl;
					this.appKey = _data.appKey;
					this.sysInfo['appKey'] = _data.appKey;
					/**
					 * 监听页面是否加载成功
					 */
					this.judgeUrlEffective(_data.httpUrl+'/ysdAppRender/mobilePageVisit?isMobile=Y&appKey='+_data.appKey);
				}else{
					uni.redirectTo({
						url: '/pages/scanCode/scanCode'
					});
				}
			}else{
				this.sysInfo['appKey'] = this.appKey;
			}
		},
		mounted(){
			/** H5通讯监听 */
			// #ifdef H5
			this.$nextTick(()=>{
				if(!window.isLoadMessage){
					window.addEventListener('message', (event) => {
						if(this.httpUrl.startsWith(event.origin)){
							let msgData = event.data.data.arg;
							this.webMessageHandle(msgData);
						}
					})
				}
				window.isLoadMessage = true;
			}) 
			// #endif 
		},
		methods: {
			//接收消息发送消息
			handleMessage: function(event) {
				let msgData = event.detail.data[0];
				this.webMessageHandle(msgData); 
			},
			//发送消息
			sendMessage: function (busName,data){
				/** H5 向网页推送消息 */
				// #ifdef H5
				let appWebView = document.getElementById("appWebView");
				appWebView.contentWindow.postMessage({ 
					type: 'UNI-APP-SEND',
					key: this.appKey,
					data: data,
					busName: busName
				},this.httpUrl);
				// #endif
				/** 除H5外 向网页推送消息 */
				// #ifndef H5
					const currentWebview  = this.$scope.$getAppWebview().children()[0];
					let sendData = {
						type: 'UNI-APP-SEND',
						key: this.appKey,
						data: data,
						busName: busName
					}
					currentWebview.evalJS(`receiveFromUniApp(${JSON.stringify(sendData)})`);
				// #endif
			},
			judgeUrlEffective: function (url){
				let that = this;
				uni.request({
				  url,
				  method: 'GET',
				  timeout: 5000,
				  dataType: 'text',
				  responseType: 'text',
				  fail:(res) => {
					that.handleError()
				  }
				});
			},
			handleError: function (){
				uni.showModal({
					title: '提示',
					content: '移动端地址绑定失败，是否重新绑定！',
					showCancel: false,
					success (res) {
						if (res.confirm) {
							uni.removeStorageSync('appInfoKey');
							uni.redirectTo({
								url: '/pages/scanCode/scanCode'
							});
						}
					}	
				});
			}
		}
	}
</script>

<style>
	
</style>
